//
//  WZSearchMessageResultController.m
//  WZEfengAndEtong
//
//  Created by 李旭 on 16/5/12.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "WZSearchMessageResultController.h"
#import "CCHatRecordCell.h"
#import "UIBarButtonItem+category.h"
#import "UINavigationItem+CustomBarButtonItem.h"
#import "CHChatRoomBase.h"
#import "CGroupMgr.h"
#import "CContactMgr.h"
#import "CMessageModel.h"
#import "IMSearchChatRecordController.h"
#import "CMessageMgr.h"
@interface WZSearchMessageResultController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@end

@implementation WZSearchMessageResultController
-(instancetype)initWithSearchText:(NSString *)searchText
{
    self = [super init];
    if (self) {
        self.searchText = searchText;
        self.messageArray = [NSMutableArray array];
        self.isChatRecord = NO;
       
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
     self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:@"call_back" title:@"返回" target:self sel:@selector(back) titleFont:Font18 titleColor:WhiteColor];
    [self.navigationItem setNewTitleCenter:@"消息记录"];
    CGRect tableFrame = CGRectMake(0.0f, 0.0f, HARDWARE_SCREEN_WIDTH, HARDWARE_SCREEN_HEIGHT - 64);
//    CGRect tableFrame = self.view.frame;
    _tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
    _tableView.frame = tableFrame;
    _tableView.backgroundColor = [UIColor clearColor];
    //    _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.decelerationRate = 0.7;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.messageArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CMessageFrame *messageFrame = self.messageArray[indexPath.row];
    CCHatRecordCell *cell = [CCHatRecordCell chatRecordCellWithTableView:tableView];
    [cell setCMessageFrame:messageFrame withSearchText:self.searchText];

    return cell;

}
-(void)viewWillAppear:(BOOL)animated
{
    [self.tableView reloadData];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CMessageFrame *messageFrame = self.messageArray[indexPath.row];
    if (self.isChatRecord == NO) {
        CHChatRoomBase *chat = [[CHChatRoomBase alloc] init];
        if (messageFrame.message.GroupId.length>3) {
            chat.group = [[[CGroupMgr alloc]init] loadGroup:messageFrame.message.GroupId];
            chat.member = nil;
        }
        else
        {
            chat.group = nil;
            chat.member = [[[CContactMgr alloc]init] loadContactWithUserId:messageFrame.message.FriendUserId];
        }
        
        UINavigationController *nav = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;
        
        [nav pushViewController:chat animated:YES];
    }
    else
    {
        IMSearchChatRecordController *search = [[IMSearchChatRecordController alloc] init];
        
        CMessageModel *mo = [[[CMessageMgr alloc]init] loadMessageWithMessageId:messageFrame.message.messageId];
        search.message = mo;
        if (messageFrame.message.GroupId.length>3) {
            search.group = [[[CGroupMgr alloc]init] loadGroup:messageFrame.message.GroupId];
            search.member = nil;
        } else {
            search.member = [[[CContactMgr alloc]init] loadContactWithUserId:messageFrame.message.FriendUserId];
            search.group = nil;
        }
        [self.navigationController pushViewController:search animated:YES];

    }

}
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
